<template>
  <el-dialog v-if="load" title="上传合同文件" v-model="visible" append-to-body :close-on-click-modal="false" :z-index="2000" width="500px" @close="handleClose">
    <VUpload v-model="form.file" title="上传文件" :types="['xlsx', 'xml']" :limit="1" :inline="1">
      图片类型只能是xml/xlsx，不超过5M
    </VUpload>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="loadings.submit" @click="submit">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import api from '../api'
import { ElMessage } from 'element-plus'

const props = defineProps({
  options: { type: Object, default: () => ({}) },
})
const emit = defineEmits(['close', 'success'])

const load = ref(false)
const visible = ref(false)
let loadings = reactive({})
const form = ref({})

// 打开弹窗
const open = async (ct_id) => {
  if (!load.value) {
    load.value = true
    await 1
  }
  loadings = reactive({})
  form.value = { ct_id, file: [] }
  visible.value = true
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
}
// 提交
const submit = () => {
  const {ct_id, file} = form.value
  if(!file?.length) {
    ElMessage.error('请先上传文件！')
    return
  }
  loadings.submit = true
  api.updateTemplateUrl({ct_id, template_url: file[0]}).then(res => {
    ElMessage.success('操作成功')
    emit('success')
    close()
  }).finally(() => {
    loadings.submit = false
  })
}

defineExpose({ open })
</script>